#include "user_spinlock.c"

struct uthreadqueue
{
    struct uthread* waitingThreads[MAX_UTHREADS];		// queue
    int head;			// index of the next free slot in queue 
    int tail;				// index of the oldest proccess in queue
    struct user_spinlock lock;
};

void initQueue(struct uthreadqueue* queue)
{
    queue->head = 0;
    queue->tail = 0;
    initlock(&queue->lock);
}

void addToQueue(struct uthreadqueue* queue, struct uthread* thread)
{
    queue->waitingThreads[queue->head] = thread;
    queue->head++;
    if(queue->head == MAX_UTHREADS)
	 queue->head = 0;
}

struct uthread* removeFromQueue(struct uthreadqueue* queue)
{
    struct uthread* oldestThread = queue->waitingThreads[queue->tail];
     queue->tail++;
    if(queue->tail == MAX_UTHREADS)
	 queue->tail = 0;
    return oldestThread;
}